import { login, loginApi } from '@/api/home';
import router from '@/router';
import { setToken, getToken, removeToken, saveTime } from '@/utils/auth';
import { Message } from 'element-ui';

export default {
    getters: {},
    namespaced: true, // 命名空间
    // getters,
    state: {
        user: { username: '', password: '' },
        userInfo: {},
        openid: '',
        spread_id: 0,
        continuous: [],
        token: getToken(), // 初始化读取本地的token
    },
    mutations: {
        //同步 也就是必须操作完成再进行下一步
        //commit的作用，就是调用store中某个mutation的函数。
        //修改操作state的数据，要通过mutations
        //更新video剧的信息

        updateToken(state, token) {
            state.token = token;
            // 存一份token到cookie
            setToken(token);
        }, // 保存用户信息
        updateUserInfo(state, info) {
            state.userInfo = info;
        },
        // 删除token
        deleteToken(state) {
            state.token = null;
            removeToken();
        },
        // 删除用户信息
        deleteUserInfo(state) {
            state.userInfo = {};
        },
        mut_userInfo(state, infoObj) {
            if (typeof infoObj == 'object' && infoObj != null) {
                state.userInfo = infoObj;
                // vuex存进userInfo缓存
                uni.setStorageSync('userInfo', state.userInfo);
            }
        },
        mut_openid(state, openidString) {
            if (typeof openidString == 'string' && openidString) {
                state.openid = openidString;
                uni.setStorageSync('openid', state.openid);
            }
        },
        mut_spread_id(state, spread_id) {
            if (typeof spread_id == 'number') {
                state.spread_id = spread_id;
                uni.setStorageSync('spread_id', state.spread_id);
            }
        },
        mut_continuous(state, cArr) {
            if (Array.isArray(cArr)) {
                if (cArr.length) {
                    state.continuous = cArr;
                    uni.setStorageSync('continuous', state.continuous);
                }
            }
        },
    },
    actions: {
        async loginAdmin(context, form) {
            // 调用登录接口
            try {
                const res = await loginApi(form);
                console.log('res  ----->  ', res);
                // if (!res) return;
                // 存储token
                context.commit('updateToken', res.data.token);

                // 存储token时间
                saveTime();
                router.push('/');
            } catch (error) {
                Message.error(error);
            }
        },
        // 获取用户信息
        async getUserInfo(context) {
            const res = await getInfo();
            const basics = await getUserBasics(res.userId);
            // 提交用户信息mutation
            // 合并头像信息和用户名信息
            context.commit('updateUserInfo', { ...res, ...basics });
        },
        //异步
        //也就是不管操作是否完成，都是立即去下一步
        //dispatch的作用，专门用来触发action
        //初始化获取用户完整连续看了多少集视频
        act_continuous({ state, dispatch }) {
            console.log('初始化获取用户完整连续看了多少集视频');
            state.continuous = uni.getStorageSync('continuous') || [];
        },
        //初始化获取用户openid
        act_openid({ state, dispatch }) {
            console.log('初始化获取用户openid');
            state.openid = uni.getStorageSync('openid') || '';
        },
        //初始化获取spread_id
        act_spread_id({ state, dispatch }) {
            console.log('初始化获取spread_id');
            state.spread_id = uni.getStorageSync('spread_id') || 0;
        },
        //初始化获取用户信息
        act_userInfo({ state, dispatch }) {
            console.log('初始化获取用户信息');
            state.userInfo = uni.getStorageSync('userInfo') || {};
        },
    },
};
